<?php
if (!defined('INWEB')) header("Location: ../index.php");
$a=getVar('a');
$id=getVar('id');
includeLang('brokers');
switch($a)
{
    case 'add':
    if($_POST)
    {
        try
        {
            $name=getVar('name');
            $abbr=getVar('abbr');

            $sql->query("SELECT * FROM brokers WHERE abbr='$abbr';");
            if($sql->row_count)
            {
                msg($Lang['error'],$Lang['broker_exists'], 'error');
            }
            else
            {
                $sql->query("INSERT INTO brokers (`name`, `abbr`) VALUES ('$name', '$abbr');");
                msg($Lang['success'],$Lang['broker_added'].'</br> <a href="?p='.$page.'">'.$Lang['back'].'</a>');
            }
        }
        catch(Exception $e)
        {
            msg($Lang['error'],$e->getMessage(),'error');
        }
    }
    else
    {
        echo '<form action="?p='.$page.'&a=add" method="post"><table border="1">';
        echo '<tr><th>'.$Lang['name'].'</th><td><input type="text" name="name" /></td></tr>';
        echo '<tr><th>'.$Lang['abbr'].'</th><td><input type="text" name="abbr" /></td></tr>';
        echo'</table><input type="submit" class="yt_button" value="'.$Lang['add'].'" /></form>';
    }
    break;
    case 'edit':
        if($_POST)
        {
            try
            {
                $name=getVar('name');
                $sql->query("UPDATE brokers SET `name`='$name' WHERE id='$id';");
                msg($Lang['success'],$Lang['broker_updated'].'</br> <a href="?p='.$page.'">'.$Lang['back'].'</a>');
                
            }
            catch(Exception $e)
            {
                msg($Lang['error'],$e->getMessage(),'error');
            }
        }
        else
        {
            $sql->query("SELECT * FROM brokers WHERE id='$id';");
            $brk=$sql->fetch_array();
            echo '<form action="?p='.$page.'&a=edit&id='.$id.'" method="post"><table border="1">';
            echo '<tr><th>'.$Lang['name'].'</th><td><input type="text" name="name" value="'.$brk['name'].'" /></td></tr>';
            echo'</table><input type="submit" class="yt_button" value="'.$Lang['edit'].'" /></form>';
        }
    break;
    case 'delete':
        if(isset($_GET['confirm']))
        {
            $sql->query("DELETE FROM brokers WHERE id='$id';");
            msg($Lang['success'],$Lang['broker_deleted']);
        }
        else
        {
            msg($Lang['warning'],$Lang['are_u_sure_delete_broker'].$id.' ?<br /> <a href="?p='.$page.'&a=delete&id='.$id.'&confirm">'.$Lang['yes'].'</a>/<a href="?p='.$page.'">'.$Lang['no'].'</a>','warning');
        }
    break;
    default:
        echo '<a href="?p='.$page.'&a=add"><img src="img/add.png" alt="'.$Lang['add'].'" title="'.$Lang['add'].'" /></a><table border="1">';
        echo '<tr><td>'.$Lang['name'].'</td><td>'.$Lang['abbr'].'</td><td>'.$Lang['actions'].'</td></tr>';

        $brokers=$sql->query("SELECT `id`, `name`, `abbr` FROM `brokers`;");
        while($brk=$sql->fetch_array($brokers))
        {
            echo '<tr><td>'.$brk['name'].'</td><td>'.$brk['abbr'].'</td><td><a href="?p='.$page.'&a=edit&id='.$brk['id'].'"><img src="img/edit.png" alt="'.$Lang['edit'].'" title="'.$Lang['edit'].'" /></a><a href="?p='.$page.'&a=delete&id='.$brk['id'].'"><img src="img/delete.png" alt="'.$Lang['delete'].'" title="'.$Lang['delete'].'" /></a></td></tr>';
        }
        echo '</table>';
    break;
}
?>